package com.example.smartall.ui;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.smartall.R;
import com.example.smartall.adapter.SplashVpAdapter;
import com.example.smartall.fragment.AllServiceFragment;
import com.example.smartall.fragment.DAFragment;
import com.example.smartall.fragment.HomeFragment;
import com.example.smartall.fragment.MineFragment;
import com.example.smartall.fragment.SmartLiveFragment;
import com.example.smartall.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

 /**
  * @Author:         abbas
  * @Description:    主页
  *
  *                  1个vp 5个fragment
  *
  *                  真 工具人类
  */

public class HomeActivity extends BaseActivity {
    //底部导航栏 由一个RadioGroup实现
    private RadioGroup rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private RadioButton rb5;
    //暴露出去的vp 为homeFragment 更多服务点击使用
    public ViewPager vpHome;

    private List<Fragment> fragments = new ArrayList<>();
    private SplashVpAdapter adapter;

    private HashMap<Integer,Integer> RGHanlderMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        init();
        initEvent();
    }

     @Override
     protected void onResume() {
         super.onResume();
         if (RGHanlderMap == null){
             RGHanlderMap = new HashMap<>();
             RGHanlderMap.put(R.id.rb1,0);
             RGHanlderMap.put(R.id.rb2,1);
             RGHanlderMap.put(R.id.rb3,2);
             RGHanlderMap.put(R.id.rb4,3);
             RGHanlderMap.put(R.id.rb5,4);
         }
     }

     public void init() {
        rg = (RadioGroup) findViewById(R.id.rg);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);
        rb5 = (RadioButton) findViewById(R.id.rb5);
        vpHome = (ViewPager) findViewById(R.id.vp_home);
        //额外传递自身
        fragments.add(new HomeFragment(this));
        fragments.add(new AllServiceFragment());
        fragments.add(new SmartLiveFragment());
        fragments.add(new DAFragment());
        fragments.add(new MineFragment());

        adapter = new SplashVpAdapter(getSupportFragmentManager(),0,fragments,this);
        vpHome.setAdapter(adapter);

        vpHome.setCurrentItem(0);
        //RadioGroup默认选中第一个 也即是第一页
        rg.check(rg.getChildAt(0).getId());
    }

    public void initEvent() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (RGHanlderMap != null){
                    if (RGHanlderMap.containsKey(i)){
                        vpHome.setCurrentItem(RGHanlderMap.get(i)); //Unboxing of 'RGHanlderMap.get(i)' may produce 'NullPointerException'
                    }
                }
            }
        });
        vpHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //TODO 页面滑动完成
            }

            @Override
            public void onPageSelected(int position) {
                rg.check(rg.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}